package com.example.communitypro.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.communitypro.pojo.LoginLog;
import com.example.communitypro.pojo.LoginLogList;
import com.example.communitypro.pojo.UserInformation;
import com.example.communitypro.service.UserInformationService;
import com.example.communitypro.util.DateUtil;
import com.example.communitypro.util.JWTUtils;
import com.example.communitypro.util.SpringContextUtils;
import com.example.communitypro.util.result.Result;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HandshakeCompletedEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/userInformation")
public class UserInformationController {


    @Autowired
    private UserInformationService userInformationService;

    @ResponseBody
    @RequestMapping(value = "/loginIn")
    @CrossOrigin
    public Result login(String userAccount, String userPassword,HttpServletRequest request){
        UserInformation userInformation=userInformationService.login(userAccount,userPassword);
        if (userInformation != null){
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String userNo = Integer.toString(userInformation.getUserNo());
            String state = Integer.toString(userInformation.getUserStatus());
            //request.getSession().setAttribute(Integer.toString(userInformation.getUserNo()), userInformation);//登录成功，将用户数据放入到Session中
            //request.getSession().setMaxInactiveInterval(3600);
            //response.sendRedirect("/Web/index.jsp");//
            try {
                String token = JWTUtils.getToken(userNo,state);
                System.out.println(token);
                Map<String, String> maps = new HashMap<>();
                maps.put("userNo",userNo);
                maps.put("token",token);

                /**
                 * lsl
                 */
                HttpSession session = request.getSession();
                session.setAttribute("userNo",userInformation.getUserNo());
                session.setAttribute("token", token);
                if (session.getAttribute("userCount")!=null){
                    Integer userCount = (Integer) session.getAttribute("userCount");
                    session.setAttribute("userCount", userCount++);
                }else {
                    session.setAttribute("userCount", 1);
                }
                LoginLogList loginLogList = (LoginLogList) SpringContextUtils.getBean("loginLogList");
                LoginLog loginLog = new LoginLog(userAccount, "用户级别为:" + userInformation.getUserStatus(), DateUtil.getTimeByTransformation(new Date(), "yyyy-MM-dd HH:mm:ss"));
                loginLogList.getLoginLogList().add(loginLog);

                return  Result.ok().data("success",maps);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            return Result.error().message("用户名或账号错误");
        }
        return Result.error().message("错误");
    }


    @ResponseBody
    @RequestMapping(value = "/loginOut")
    @CrossOrigin
    public String loginout(String userAccount,String userPassword){
        UserInformation userInformation=userInformationService.login(userAccount,userPassword);
        if(userInformation != null){
            //request.getSession().removeAttribute(Integer.toString(userInformation.getUserNo()));
            //response.sendRedirect("/Web/index.jsp");
            return "login";//返回到登陆界面
        }else{
            return "error";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/addUserInformation")
    @CrossOrigin
    public Result addUserInformation(UserInformation record){
        return userInformationService.addUserInformation(record);
    }

    @ResponseBody
    @RequestMapping(value = "/dropUserInformation")
    @CrossOrigin
    public Result dropUserInformation(Integer userNo){
        return userInformationService.dropUserInformation(userNo);
    }

    @ResponseBody
    @RequestMapping(value = "/modifyUserInformation")
    @CrossOrigin
    public Result modifyUserInformation(UserInformation record){
        return userInformationService.modifyUserInformation(record);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserInformation")
    @CrossOrigin
    public Result queryUserInformation(Integer userNo){
        return userInformationService.queryUserInformation(userNo);
    }

    @ResponseBody
    @RequestMapping(value = "/selectUserByKey")
    @CrossOrigin
    public Result selectUserByKey(String keyword){
        return userInformationService.selectUserByKey(keyword);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPassUser")
    @CrossOrigin
    public Result queryPassUser(Integer user_status){
        return userInformationService.queryPassUser(user_status);
    }

    @ResponseBody
    @RequestMapping(value = "/count")
    @CrossOrigin
    public Integer count(){
        return userInformationService.count();
    }

    @ResponseBody
    @RequestMapping(value = "/findAllUser")
    @CrossOrigin
    public Result findAllUser(){
        return userInformationService.findAllUser();
    }
}
